package com.iwhalecloud.bss.kite.cucc.web.controller.scene;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.cucc.client.api.inst.IAdditionalOfferInstService;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.web.service.ISceneService;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.transaction.KiteTransactional;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.ztesoft.bss.common.util.ContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *
 * @author zou.qinghua
 *
 */
@RestController("cuccSceneReload")
@RequestMapping("/cuccSceneReload")
@Api(tags = {
    "/cuccSceneReload - 场景受理重载"
})
public class SceneReloadController {

	@Autowired
	private ISceneService sceneReloadService;
    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IAdditionalOfferInstService additionalOfferInstService;


	@KiteTransactional
    @ApiOperation(value = "场景重载", notes = "场景重载，返回场景实例标识")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "orderId", value = "订单标识", required = true),
        @ApiImplicitParam(name = "serviceOfferId", value = "服务提供，核查转定传2，订单签订传3，传输计划变更传4", required = true)
    })
    @GetMapping("/reload")
    public DataResult<String> reloadOfferScene(@RequestParam String orderId, @RequestParam String serviceOfferId) {
        /*DataResult<String> result= sceneReloadService.reloadOfferScene(orderId, serviceOfferId, KiteWebContext.getContext().getCustId(), KiteWebContext.getContext().getCust().getCustName(),
                ContextUtil.getUserId().toString(), ContextUtil.getLandId(), ContextUtil.getLoginInfo().getUserInfo().getPostRegionId().toString());
        initAdditionalOffer(serviceOfferId);*/
        return  null;
    }

    /***
     * 初始化可选资费包  //迁移到TransferPlanController类，在删除传输计划时调用
     * @param serviceOfferId
     */
    private void initAdditionalOffer(String serviceOfferId){
        //主商品实例ID
        String  offerInstId = this.querySceneInstService.getExtParameterValue(KiteWebContext.getContext().getCurrentSceneInstId(), "mainOfferInstId");

        List<KiteProdInst> prodInsts = querySceneInstService.listProdInstsFromOfferInst(KiteWebContext.getContext().getCustId(), offerInstId);

        DcPublicCache.getByPkey(LocalKeyConsts.ADD_OFFER_STYPE, serviceOfferId).stream().forEach(dcPublic -> {
            additionalOfferInstService.addAdditionalOffer(KiteWebContext.getContext().getCurrentSceneInstId(), prodInsts.get(0).getProdInstId(), offerInstId, dcPublic.getPcode(), KiteWebContext.getContext().getCustId(),
                "1", true);
        });
    }
}
